//
//  commodityProfileSuffix.m
//  代码布局
//
//  Created by iOS on 16/7/28.
//  Copyright © 2016年 iOS. All rights reserved.
//

#import "commodityProfileSuffix.h"

@implementation commodityProfileSuffix
+ (UIImage*)imageFromString:(NSString*)string{
    NSDictionary *attrs1 = @{NSFontAttributeName : [UIFont systemFontOfSize:10]};
    CGSize size1 = [string boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs1 context:nil].size;
 CGSize size = CGSizeMake(size1.width*1.5, size1.height*1.5);
    CGRect cubeRect = CGRectMake(3, 2, size.width+6, size.height + 4);
    
    
    UIGraphicsBeginImageContext(cubeRect.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextDrawPath (ctx, kCGPathStroke );
    CGContextFillRect(ctx, CGRectMake(0, 0, size.width, size.height));
    
    

    UIGraphicsBeginImageContext(cubeRect.size);
        NSString *str = string;
           
        NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
        // NSForegroundColorAttributeName : 文字颜色
        // NSFontAttributeName : 字体
        attrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
    attrs[NSBackgroundColorAttributeName] = [UIColor redColor];
        attrs[NSFontAttributeName] = [UIFont fontWithName:@"AmericanTypewriter-Bold" size:8.0f];
        [str drawInRect:CGRectMake(0, 0, size.width+6, size.height + 4) withAttributes:attrs];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    
    
    
    return img;//获得添加水印后的图片;
}

+ (UIImage *)watermarkImage:(NSString *)text{
    //生成红色图片
    NSDictionary *attrs1 = @{NSFontAttributeName : [UIFont systemFontOfSize:fontNunber(10)]};
    CGSize size1 = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs1 context:nil].size;
    CGSize size = CGSizeMake(size1.width*1.2, size1.height*1.2);
    CGRect cubeRect = CGRectMake(0, 0, size.width, size.height);
    
    
    UIGraphicsBeginImageContext(cubeRect.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextDrawPath (ctx, kCGPathStroke );
    CGContextSetFillColorWithColor(ctx, [[UIColor redColor] CGColor]);
    CGContextFillRect(ctx, CGRectMake(0, 0, size.width, size.height));
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
//图片圆角
    
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    [[UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, size.width, size.height) cornerRadius:size1.height/8.0] addClip];
    [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
    
//    [[UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, size.width, size.height) cornerRadius:size1.height/8.0] addClip];
//    [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
    
    
    UIImage * roundImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    
    
    
    //图片打水印
    //1.获取上下文
    
    UIGraphicsBeginImageContext(size);
    
    
    
    //2.绘制图片
    
    [roundImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
    
    
    
    //3.绘制水印文字
    
    CGRect rect = CGRectMake((size.width - size1.width)/2.0, (size.height - size1.height)/2.0, size1.width, size1.height);
    
    
    
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
    
    style.alignment = NSTextAlignmentCenter;
    
    //文字的属性
    
    NSDictionary *dic = @{
                          
                          NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter-Bold" size:fontNunber(10.0)],
                          
                          NSParagraphStyleAttributeName:style,
                          
                          NSForegroundColorAttributeName:[UIColor whiteColor],
                        //  NSStrokeWidthAttributeName:@(5.0)
                          
                          };
    
    //将文字绘制上去
    
    [text drawInRect:rect withAttributes:dic];
    
    
    
    //4.获取绘制到得图片
    
    UIImage *watermarkImage = UIGraphicsGetImageFromCurrentImageContext();
    
    
    
    //5.结束图片的绘制
    
    UIGraphicsEndImageContext();
    
    
    
    return watermarkImage;
    
}
@end
